/*
 * Created by JFormDesigner on Sat Nov 02 11:30:22 CST 2024
 */

package com.hafu.view.RuKu;

import com.hafu.model.GysInfo;
import com.hafu.model.RuKuDetail;
import com.hafu.model.RuKuMain;
import com.hafu.model.SpInfo;
import com.hafu.service.GysInfoService;
import com.hafu.service.RuKuService;
import com.hafu.service.SpInfoService;
import com.hafu.util.StringUtil;

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.table.*;

/**
 * @author zk
 */
public class AddRuKuFrm extends JFrame {
    private GysInfoService gysInfoService = new GysInfoService();
    private SpInfoService spInfoService = new SpInfoService();
    private RuKuService ruKuService = new RuKuService();
    private TableColumn tableColumn;
    public AddRuKuFrm() {
        initComponents();
        fillComboBox();
    }

    private void fillComboBox() {
        ArrayList<GysInfo> gysInfoByName = gysInfoService.getGysInfoName(new GysInfo());
        for (GysInfo gysInfo : gysInfoByName){
            RuKucomboBox1.addItem(gysInfo.getName());
        }
        JComboBox<SpInfo> comboBox = new JComboBox<>();
        ArrayList<SpInfo> spInfoByName = spInfoService.getSpInfoByName(new SpInfo());
        for (SpInfo spInfo : spInfoByName){
            comboBox.addItem(spInfo);
        }


        DefaultTableModel defaultTableModel = (DefaultTableModel) table1.getModel();
        tableColumn = table1.getColumnModel().getColumn(0);
        tableColumn.setCellEditor(new DefaultCellEditor(comboBox));

    }

    private void RukuTianJia(ActionEvent e) {
        // TODO add your code here 给表格添加新行
        //获取表格
        DefaultTableModel defaultTableModel = (DefaultTableModel) table1.getModel();
        defaultTableModel.addRow(new Vector());
    }

    private void RuKuTiJiao(ActionEvent e) throws SQLException {
        // TODO add your code here
        RuKuMain rukuMain = new RuKuMain();
        rukuMain.setRukuName(RuKutextField1.getText());
        rukuMain.setGysName(RuKucomboBox1.getSelectedItem().toString());
        //遍历表格的每一行
        DefaultTableModel defaultTableModel = (DefaultTableModel) table1.getModel();
        int rowCount = defaultTableModel.getRowCount();
        ArrayList<RuKuDetail> ruKuDetails = new ArrayList<>();
        BigDecimal totalPrice = new BigDecimal(0);
        if (StringUtil.isEmpty(rukuMain.getRukuName())){
            JOptionPane.showMessageDialog(null,"入库单名称不能为空");
            return;
        }
        for (int i = 0; i < rowCount; i++) {
            SpInfo SpInfo1 = (SpInfo)table1.getValueAt(i, 0);
            if (SpInfo1 == null){
                continue;
            }
            if (StringUtil.isEmpty(SpInfo1.getName())){
                continue;
            }
            String s = table1.getValueAt(i, 1).toString();
            Integer num = Integer.parseInt(s);
            RuKuDetail ruKuDetail = new RuKuDetail();
            ruKuDetail.setSpName(SpInfo1.getName());
            ruKuDetail.setNum(num);
            ruKuDetail.setPrice(SpInfo1.getPrice());
            ruKuDetails.add(ruKuDetail);
            totalPrice = totalPrice.add(SpInfo1.getPrice().multiply(new BigDecimal(num)));
        }
        rukuMain.setRukudetailList(ruKuDetails);
        rukuMain.setPrice(totalPrice);
        int b = ruKuService.addRuKu(rukuMain);
        if (b != -1){
            JOptionPane.showMessageDialog(null,"入库成功");
            this.dispose();
        }else {
            JOptionPane.showMessageDialog(null,"入库失败");
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        RuKulabel1 = new JLabel();
        RuKutextField1 = new JTextField();
        RuKucomboBox1 = new JComboBox();
        RukuTianJia = new JButton();
        RuKuTiJiao = new JButton();
        Rulabel2 = new JLabel();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();

        //======== this ========
        setTitle("\u6dfb\u52a0\u5165\u5e93\u5355");
        var contentPane = getContentPane();

        //---- RuKulabel1 ----
        RuKulabel1.setText("\u5165\u5e93\u540d\u79f0");

        //---- RukuTianJia ----
        RukuTianJia.setText("\u6dfb\u52a0\u65b0\u884c");
        RukuTianJia.addActionListener(e -> {
			RukuTianJia(e);
			RukuTianJia(e);
		});

        //---- RuKuTiJiao ----
        RuKuTiJiao.setText("\u63d0\u4ea4\u5165\u5e93");
        RuKuTiJiao.addActionListener(e -> {try {
RuKuTiJiao(e);} catch (SQLException ex) {
    throw new RuntimeException(ex);
}});

        //---- Rulabel2 ----
        Rulabel2.setText("\u4f9b\u5e94\u5546\u9009\u62e9");

        //======== scrollPane1 ========
        {

            //---- table1 ----
            table1.setModel(new DefaultTableModel(
                new Object[][] {
                    {null, null},
                },
                new String[] {
                    "\u5546\u54c1", "\u6570\u91cf"
                }
            ));
            scrollPane1.setViewportView(table1);
        }

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(32, 32, 32)
                    .addComponent(RuKulabel1)
                    .addGap(18, 18, 18)
                    .addComponent(RuKutextField1, GroupLayout.PREFERRED_SIZE, 88, GroupLayout.PREFERRED_SIZE)
                    .addGap(89, 89, 89)
                    .addComponent(Rulabel2)
                    .addGap(18, 18, 18)
                    .addComponent(RuKucomboBox1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 94, Short.MAX_VALUE)
                    .addComponent(RukuTianJia)
                    .addGap(58, 58, 58)
                    .addComponent(RuKuTiJiao)
                    .addGap(30, 30, 30))
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(38, 38, 38)
                    .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 690, GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(55, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(23, 23, 23)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(RuKulabel1)
                        .addComponent(RuKuTiJiao)
                        .addComponent(RukuTianJia)
                        .addComponent(RuKutextField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(RuKucomboBox1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(Rulabel2))
                    .addGap(18, 18, 18)
                    .addComponent(scrollPane1, GroupLayout.DEFAULT_SIZE, 298, Short.MAX_VALUE)
                    .addGap(34, 34, 34))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel RuKulabel1;
    private JTextField RuKutextField1;
    private JComboBox RuKucomboBox1;
    private JButton RukuTianJia;
    private JButton RuKuTiJiao;
    private JLabel Rulabel2;
    private JScrollPane scrollPane1;
    private JTable table1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
